package com.woniuxy.cq.phrase3.common;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.util.Random;

/**
 * @Author Daniel
 * @Description //密码加密工具
 * @Date 18:56 2020/9/3
 **/
public class EncryptUtil {

    public static  final  String ALGORITHM_NAME="SHA-256";
    public static  final  int HASH_ITERATIONS=1;

    /**
     * 加密方法
     * @param password
     * @return
     */
    public static String encrypt(String password){
        return encrypt(password,"");
    }


    /**
     * 加密方法
     * @param password
     * @return
     */
    public static String encrypt(String password,String salt){
        SimpleHash simpleHash = new SimpleHash(ALGORITHM_NAME,password, ByteSource.Util.bytes(salt),HASH_ITERATIONS);
        return simpleHash.toHex();
    }

    public static String getSalt(int n){
        char[] chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
                "1234567890!@#$%^&*()_+").toCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++){
            //Random().nextInt()返回值为[0,n)
            char aChar = chars[new Random().nextInt(chars.length)];
            sb.append(aChar);
        }
        return sb.toString();
    }

}